package com.ias.agdis_r.util;

import com.ias.agdis_r.module.MessageModel;

/**
 * 消息处理
 * 
 * @author wanghu
 * 
 */
public class MyMessage {
	
	/**
	 * 获取对应字段的内容
	 * 
	 * @param _message
	 *            信息内容
	 * @param _name
	 *            字段名
	 * @param _namelen
	 *            字段名长度
	 * @return
	 */
	public static String getValue(String _message, Object _name, int _namelen) {
		String result = null;
		String name = _name.toString();
		int index = _message.indexOf("," + name);
		if (index == -1) {
			int index1 = _message.indexOf(name);
			if (index1 == 0) {
				result = _message.substring(_message.indexOf(name) + _namelen)
						.split(",")[0];
			} else {
				return null;
			}
		} else {

			int nameLen = _namelen + 1;
			result = _message
					.substring(_message.indexOf("," + _name) + nameLen).split(
							",")[0];
		}
		return result;
	}
	
	//获取TXL长度
	public static int getTxlValue(String _message, String _name) {
		int nResult = -1;
		int nNameLen = _name.length();
		
		String sTxl = getValue(_message,_name,nNameLen);
		if (S.isEmpty(sTxl)) {
			return nResult;
		} else {
			if(!S.isNumber(sTxl)) {
				return nResult;
			} else {
				nResult = Integer.parseInt(sTxl);
			}
		}
		return nResult;
	}

	public static String getTxtContent(String _message)
	{
		String content = "";
		try {
			
			int nTsl = MyMessage.getTxlValue(_message, MessageModel.TSL);
			int nTxl = MyMessage.getTxlValue(_message, MessageModel.TXL);
			int index = _message.indexOf("TXT");
			if(index!=-1)
			{
				if (nTsl > 0) {
					content =  _message.substring(index+3,index+3+nTsl);
				}
				else if(nTxl>0)
				{
					content = S.getTXTContent(_message, nTxl);
				}
			}
			
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return content;
	}
	
	public static String getPXT(String _message, String _name) {
		String result = null;
		int iLength = _name.length();
		int indexS = _message.indexOf(_name + "<!TX<");
		int indexE = _message.indexOf(">!>,",indexS);
		if ((indexS != -1)&&(indexE != -1)) {
			result = _message.substring(indexS+iLength+5, indexE);
		}
		return result;
	}

	public static String getPXT(String _message) {
		String result = null;
		String _name = "PXT";
		int iLength = _name.length();
		int indexS = _message.indexOf(_name + "<!TX<");
		int indexE = _message.indexOf(">!>,",indexS);
		if ((indexS != -1)&&(indexE != -1)) {
			result = _message.substring(indexS+iLength+5, indexE);
		}
		return result;
	}
	public static String getTxt(String _message, String _name) {
		String result = null;
		int iLength = _name.length();
		int indexS = _message.indexOf("," + _name);
		int indexE = _message.indexOf("],",indexS);
		if ((indexS != -1)&&(indexE != -1)) {
			result = _message.substring(indexS+iLength+1, indexE+1);
		}
		return result;
	}
	
	/**
	 * 获取消息号
	 * 
	 * @param _message
	 *            信息内容
	 * @return
	 */
	public static String getMS(String _message) {
		return _message.substring(2, _message.indexOf(","));
	}
	
	/**
	 * 获取消息号
	 * 
	 * @param _message
	 *            信息内容
	 * @return
	 */
	public static String getMT(String _message) {
		
		String mt = "";
		try {
			mt = S.mySubString(_message, 10, 14);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return mt;
	}
}
